package com.snow.test;

import org.apache.commons.lang3.ObjectUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChildMapUtils{

    public static <T extends ParentMap> Map<String, List<T>> sameConditionDetailsSearch(List<T> list){
        Map<String, List<T>> map = new HashMap<>();

        for(T t : list){
            String condition = t.getConditionSameDetailMerger();
            if(map.containsKey(condition)){
                map.get(condition).add(t);
            }else {
                List<T> list1 = new ArrayList<>();
                list1.add(t);
                map.put(condition, list1);
            }
        }
        return map;
    }


    public static <T extends  ParentMap> List<T> sameConditionDetailsHandle(Map<String, List<T>> listMap, BigDecimal noTaxAmountLimit){
        List<T> result = new ArrayList<>();

        for(List<T> list : listMap.values()){
            BigDecimal amountSum = BigDecimal.ZERO;
            BigDecimal diffAmountSum = BigDecimal.ZERO;
            T tTemp = list.get(0);

            int count = 1;
            int ListSize = list.size();
            boolean hanldeAll = false;
            for(T t : list){
                diffAmountSum = diffAmountSum.add(t.getParentNoTaxAmount());
                boolean flag1 = ObjectUtils.compare(diffAmountSum, noTaxAmountLimit, false) < 0;
                if(flag1){
                    //判断是否超限额，未税金额累加小于限额
                    amountSum = amountSum.add(t.getParentNoTaxAmount());
                }else {
                    //未税金额大于等于限额，处理之前的小于限额的数据
                    BigDecimal amountSumTemp = amountSum.add(t.getParentNoTaxAmount());
                    BigDecimal taillDeffer[] = getTailDiffer(amountSumTemp, t.getParentTaxRate());
                    boolean flag2 = ObjectUtils.compare(amountSumTemp, noTaxAmountLimit, false) < 0;
                    if(flag2){
                        //判断是否超限额，没有超限额，合并明细
                        amountSum = amountSumTemp;
                        tTemp.setParentNoTaxAmount(amountSum);
                        result.add(tTemp);

                        if(count == ListSize){
                            hanldeAll = true;
                            break;
                        }
                        //重新赋值对象
                        tTemp = list.get(count);
                    }else {
                        //赋值
                        tTemp.setParentNoTaxAmount(amountSum);
                        result.add(tTemp);

                        //重新赋值对象
                        tTemp = t;
                    }

                    //初始化数据
                    amountSum = t.getParentNoTaxAmount();
                }

                count++;
            }

            if(!hanldeAll){
                tTemp.setParentNoTaxAmount(amountSum);
                result.add(tTemp);
            }
        }
        return result;
    }


    /**
     * 计算尾差 价税分离
     *
     * @param sumAoumt 价税合计
     * @param taxRate  税率
     * @return 金额 税额 尾差
     */
    public static BigDecimal[] getTailDiffer(BigDecimal sumAoumt, BigDecimal taxRate) {
        BigDecimal[] result = new BigDecimal[3];
        if (sumAoumt == null || taxRate == null) {
            return result;
        }
        if(taxRate.compareTo(BigDecimal.ZERO) == 0){
            result[0] = sumAoumt;
            result[1] = new BigDecimal("0");
            result[2] = new BigDecimal("0");
            return result;
        }
        taxRate = taxRate.divide(new BigDecimal("100"));
        BigDecimal tmpRate = new BigDecimal("0");
        tmpRate = taxRate.add(new BigDecimal("1"));

        BigDecimal taxAmount = sumAoumt.divide(tmpRate,10000, RoundingMode.CEILING).multiply(taxRate).setScale(2, BigDecimal.ROUND_HALF_UP);
        BigDecimal amount = sumAoumt.subtract(taxAmount).setScale(2, BigDecimal.ROUND_HALF_UP);

        //航信、百旺接口校验算出的税额为：5629.91（未税金额）*17%（税率）≈957.08
        //尾差为957.08-957.09=﹣0.01
        BigDecimal tailDiffer = amount.multiply(taxRate).setScale(2, BigDecimal.ROUND_HALF_UP).subtract(taxAmount);
        result[0] = amount;
        result[1] = taxAmount;
        result[2] = tailDiffer;
        return result;
    }

}
